package controller;

import service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class LoginController {
    private LoginService loginService;

    public void setLoginService(LoginService loginService){
        this.loginService=loginService;
    }

    @RequestMapping("/logout")
    public String logout(HttpServletRequest request,HttpServletResponse response) throws Exception{
        request.getSession().invalidate();
        return "redirect:/login.html";
    }

    @RequestMapping("/logincheck")
    public String loginCheck(HttpServletRequest request, HttpServletResponse response,
                             RedirectAttributes redirectAttributes)throws Exception{
        int username=Integer.parseInt(request.getParameter("username"));
        String password = request.getParameter("password");
        String spwd=loginService.getStudentPasswd(username);
        String mpwd=loginService.getManagerPasswd(username);
        if (password==spwd) {
            redirectAttributes.addFlashAttribute("succ", "成功！");
            return "redirect:/main_student.jsp";
        } else if(password==mpwd){
            redirectAttributes.addFlashAttribute("succ", "成功！");
            return "redirect:/main_supermanager.jsp";
        } else {
            redirectAttributes.addFlashAttribute("succ", "失败！");
            return "redirect:/login.jsp";
        }
    }

}
